package com.jof.framework.hmc.sender.email;

import java.util.Map;

import com.jof.framework.hmc.domain.Email;
import com.jof.framework.template.TemplateEngine;

/**
 * 默认的基于模板引擎的邮件实现
 * @author Jonathan
 *
 */
public class DefaultEmailBuilder implements EmailBuilder {
	/**
	 * 默认的模板文件后缀名-使用freemarker
	 */
	private static final String DEFAULT_TEMPLATE_SUFFIX = ".ftl";
	private static final String SPLITOR = "-";
	private static final String TITLE_SUFFIX = SPLITOR + "subject";
	private static final String CONTENT_SUFFIX = SPLITOR + "content";
	
	protected TemplateEngine templateEngine;
	protected String suffix;
	
	public void setTemplateEngine(TemplateEngine templateEngine) {
		this.templateEngine = templateEngine;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getSuffix() {
		return suffix == null ? DEFAULT_TEMPLATE_SUFFIX : suffix;
	}

	
	public Email buildEmail(String templateId, Email email,Map<?, ?> parameters) {
		String subjectTemplateName = new StringBuilder(templateId).append(TITLE_SUFFIX).append(DEFAULT_TEMPLATE_SUFFIX).toString();
		String contentTemplateName = new StringBuffer(templateId).append(CONTENT_SUFFIX).append(DEFAULT_TEMPLATE_SUFFIX).toString();
		String subject = templateEngine.getText(subjectTemplateName, parameters);
		String content = templateEngine.getText(contentTemplateName, parameters);
		email.setSubject(subject);
		email.setBodyContent(content, true);
		return email;
	}

//	
//	public Email buildEmail(String templateId, Email email,Map<Object, Object> parameters, Locale locale) {
//		Assert.notNull(locale);
//		String localeString = locale.toString();
//		String subjectTemplateName = new StringBuilder(templateId).append(TITLE_SUFFIX).append(SPLITOR).append(localeString).append(DEFAULT_TEMPLATE_SUFFIX).toString();
//		String contentTemplateName = new StringBuffer(templateId).append(CONTENT_SUFFIX).append(SPLITOR).append(localeString).append(DEFAULT_TEMPLATE_SUFFIX).toString();
//		String subject = templateEngine.getText(subjectTemplateName, parameters);
//		String content = templateEngine.getText(contentTemplateName, parameters);
//		email.setSubject(subject);
//		email.setBodyContent(content, true);
//		return email;
//	}

}
